import 'package:flutter/material.dart';
import 'package:get/get.dart';
import "../../utils/screenAdapter.dart";
import '../../utils/tabbarType.dart';
import '../../modules/goodsDetails/controllers/goods_details_controller.dart';

// 顶部自定义导航区域
class MyAppBar extends GetView<GoodsDetailsController> implements PreferredSizeWidget {
  final String? initialTitle;
  final bool? centerTitle;
  final Color? backgroundColor;
  final double? elevation;
  final String showSearchFlag; // 搜索框显示标识
  final Widget? leading; // 导航栏左侧的控件
  final List<Widget>? actions; // 导航栏右侧的控件
  final Function? onSearch;
  final Function(int index)? changeTabBar; // 切换tabBar
  final Function(String?)? onChangedValue; // 搜索框内容改变时的回调
  final Function(String?)? onSubmit; // 软键盘回车键时的回调

  const MyAppBar({
    super.key,
    this.initialTitle,
    this.leading,
    this.actions,
    this.centerTitle,
    this.backgroundColor,
    this.elevation,
    required this.showSearchFlag,
    this.changeTabBar,
    this.onSearch,
    this.onChangedValue, // 搜索框内容改变时的回调
    this.onSubmit, // 软键盘回车键时的回调
  });

  // 只是用来点击跳转的搜索框
  Widget _buildTitle() {
    return InkWell(
      child: Container(
        width: ScreenAdapter.width(840),
        height: ScreenAdapter.height(96),
        decoration: BoxDecoration(
          color: Color.fromARGB(255, 255, 255, 240),
          borderRadius: BorderRadius.circular(ScreenAdapter.radius(50)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  onPressed: null,
                  icon: Icon(Icons.search, size: ScreenAdapter.fontSize(46)),
                  color: Color.fromARGB(187, 185, 185, 185),
                ),
                Text(
                  "$initialTitle",
                  style: TextStyle(
                    fontSize: ScreenAdapter.fontSize(30),
                    color: Color.fromARGB(187, 185, 185, 185),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
      onTap: () {
        // callBack();
        onSearch!();
      },
    );
  }

  Widget _buildSearchField() {
    return Container(
      width: ScreenAdapter.width(840),
      height: ScreenAdapter.height(96),
      decoration: BoxDecoration(
        color: Color.fromARGB(240, 240, 240, 240),
        borderRadius: BorderRadius.circular(50),
      ),
      child: TextField(
        onChanged: onChangedValue, // 监听输入框内容变化
        onSubmitted: onSubmit, // 监听软键盘回车键
        style: TextStyle(fontSize: ScreenAdapter.fontSize(36)), // 设置字体大小
        autofocus: true,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.all(0), // 去掉内边距
          prefixIcon: Icon(
            Icons.search,
            size: ScreenAdapter.fontSize(46),
            color: Color.fromARGB(187, 185, 185, 185),
          ),
          border: OutlineInputBorder(borderSide: BorderSide.none),
        ),
      ),
    );
  }

  Widget _buildAppBar() {
    switch (showSearchFlag) {
      case "field":
        return _buildSearchField();
      case "navigator":
        return _buildTitle();
      default:
        return _buildNormalAppBar();
    }
  }

  Widget _buildNormalAppBar() {
    
    // final GoodsDetailsController controller = Get.put(
    //   GoodsDetailsController(),
    // ); // 在widget获取页面绑定的控制器实例

    return Obx(
      () => controller.showTabBar.value
          ? SizedBox(
              // color: Color.fromRGBO(255, 255, 255, 0.5),
              width: ScreenAdapter.width(300),
              height: ScreenAdapter.height(96),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: TabBarList.values.map((item) {
                  return InkWell(
                    onTap: () {
                      print("点击了index=${item.index}");
                      print("controller.index.value=${controller.index.value}");
                      controller.onChangedIndex(item.index);
                    },
                    child: Column(
                      children: [
                        Text(
                          item.name,
                          style: TextStyle(
                            fontSize: ScreenAdapter.fontSize(32),
                          ),
                        ),
                        SizedBox(height: ScreenAdapter.height(20)),
                        Obx(
                          () => Container(
                            height: ScreenAdapter.height(6),
                            width: ScreenAdapter.width(50),
                            decoration: BoxDecoration(
                              color: controller.index.value == item.index
                                  ? Colors.orange
                                  : Colors.transparent,
                              borderRadius: BorderRadius.circular(
                                ScreenAdapter.radius(5),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                }).toList(),
              ),
            )
          : SizedBox(width: 0, height: 0),
    );
  }

  @override
  PreferredSizeWidget build(BuildContext context) {
    return AppBar(
      leading:
          leading ??
          IconButton(
            onPressed: () {
              Get.back(); // 返回上一页
            },
            icon: Icon(Icons.arrow_back_ios_new_rounded),
          ),
      backgroundColor: backgroundColor ?? Colors.transparent, // 导航栏背景颜色透明
      scrolledUnderElevation: 0, // 向下滑动时背景阴影为0
      elevation: 0, // 阴影背景高度为0
      title: _buildAppBar(),
      actions: actions ?? <Widget>[], // 操作按钮
    );
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(ScreenAdapter.height(96)); // 设置导航栏高度
}
